﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 采购原料合同录入 : System.Web.UI.Page
    {
        protected int m1 = 1;
        protected int m2 = 2;
        protected int m3 = 3;
        protected string 年度;
        protected string 季度;

        protected DateTime d1, d2, d3, d4;//d1月初日期，d2上旬截止日期,d3中旬截止日期,d4月末日期

        protected void Page_Load(object sender, EventArgs e)
        {

            int id = 0;
            int zid = 0;
            int bid = 0;
            this.Message.Text = "·准备就绪";
            string tr=Request.QueryString["id"];
            if (!this.IsPostBack)
            {
                if (Request.QueryString["in"] != null)
                {
                    ((TextBox)this.FindControl("BianHao")).ReadOnly = true;
                    ((TextBox)this.FindControl("BianHao")).CssClass = "blank_left";
                    ((TextBox)this.FindControl("Date1")).ReadOnly = true;
                    ((TextBox)this.FindControl("Date1")).CssClass = "blank_left";
                    ((TextBox)this.FindControl("Date2")).ReadOnly = true;
                    ((TextBox)this.FindControl("Date2")).CssClass = "blank_left";
                    //((TextBox)this.FindControl("ShuLiang")).ReadOnly = true;
                    //((TextBox)this.FindControl("ShuLiang")).CssClass = "blank_left";
                    ((TextBox)this.FindControl("DanJia")).ReadOnly = true;
                    ((TextBox)this.FindControl("DanJia")).CssClass = "blank_left";
                    //((TextBox)this.FindControl("JiaoHuo")).ReadOnly = true;
                    //((TextBox)this.FindControl("JiaoHuo")).CssClass = "blank_left";
                    ((TextBox)this.FindControl("YunShu")).ReadOnly = true;
                    ((TextBox)this.FindControl("YunShu")).CssClass = "blank_left";
                    ((TextBox)this.FindControl("BeiZhu")).ReadOnly = true;
                    ((TextBox)this.FindControl("BeiZhu")).CssClass = "blank_left";
                    for (int i = 1; i < 13; i++)
                    {
                        ((TextBox)this.FindControl("JiaoHuo"+i)).ReadOnly = true;
                        ((TextBox)this.FindControl("JiaoHuo"+i)).CssClass = "blank_left";
                    }     
                }

                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (Request.QueryString["ZID"] != null)
                {
                    int.TryParse(Request.QueryString["ZID"], out zid);
                }
                if (Request.QueryString["BID"] != null)
                {
                    int.TryParse(Request.QueryString["BID"], out bid);
                }
                if (id > 0)
                {
                    this.HTID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.HTID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                if (zid > 0)
                {
                    this.Button_Cancel.Visible = true;
                    this.Button_Cancel.OnClientClick = "parent.swin2_load('/Sale/采购原料中标.aspx?id=" + bid + "',840,422);return false;";

                   // this.Button_Close.Visible = false;

                    this.Date1.Text = DateTime.Today.ToLongDateString();
                    this.Date2.Text = this.Date1.Text;
                    this.ZBID.Text = zid.ToString();
                    this.SqlDataSource1.SelectParameters["中标ID"].DefaultValue = this.ZBID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                if (LeiBie.Text == "套管光管" || LeiBie.Text == "套管光管(N80)")
                {
                    this.ChangDu.Text = "9.6~10.3";
                }
                if (LeiBie.Text == "油管光管" || LeiBie.Text == "油管光管(N80)")
                {
                    this.ChangDu.Text = "9.2~9.7";
                }
                if (LeiBie.Text == "油管接箍光管" || LeiBie.Text == "套管接箍光管")
                {
                    this.ChangDu.Text = "6~10";
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (this.HTID.Text != "0")
            {
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.BianHao.Text = (string)e.Command.Parameters[1].Value;
                    this.LBID.Text = ((int)e.Command.Parameters[3].Value).ToString();
                    this.GHID.Text = ((int)e.Command.Parameters[4].Value).ToString();

                    this.NianDu.Text = ((int)e.Command.Parameters[5].Value).ToString();
                    this.JiDu.Text = ((byte)e.Command.Parameters[6].Value).ToString();
                    this.NianJiDu.Text = this.NianDu.Text + "年" + this.JiDu.Text + "季度";
                    this.ShuLiang.Text = ((double)e.Command.Parameters[7].Value).ToString();
                    this.DanJia.Text = ((double)e.Command.Parameters[8].Value).ToString();
                    this.ChangDu.Text = ((string)e.Command.Parameters[9].Value).ToString();

                    this.Date1.Text = ((DateTime)e.Command.Parameters[10].Value).ToLongDateString();
                    this.Date2.Text = ((DateTime)e.Command.Parameters[11].Value).ToLongDateString();
                    this.YunShu.Text = (string)e.Command.Parameters[12].Value;
                    this.BeiZhu.Text = (string)e.Command.Parameters[13].Value;
                    this.JiaoHuo.Text = ((double)e.Command.Parameters[14].Value).ToString();

                    this.JiaoHuo1.Text = ((double)e.Command.Parameters[15].Value).ToString();
                    this.JiaoHuo2.Text = ((double)e.Command.Parameters[16].Value).ToString();
                    this.JiaoHuo3.Text = ((double)e.Command.Parameters[17].Value).ToString();
                    this.JiaoHuo4.Text = ((double)e.Command.Parameters[18].Value).ToString();
                    this.JiaoHuo5.Text = ((double)e.Command.Parameters[19].Value).ToString();
                    this.JiaoHuo6.Text = ((double)e.Command.Parameters[20].Value).ToString();
                    this.JiaoHuo7.Text = ((double)e.Command.Parameters[21].Value).ToString();
                    this.JiaoHuo8.Text = ((double)e.Command.Parameters[22].Value).ToString();
                    this.JiaoHuo9.Text = ((double)e.Command.Parameters[23].Value).ToString();
                    this.JiaoHuo10.Text = ((double)e.Command.Parameters[24].Value).ToString();
                    this.JiaoHuo11.Text = ((double)e.Command.Parameters[25].Value).ToString();
                    this.JiaoHuo12.Text = ((double)e.Command.Parameters[26].Value).ToString();
               


                    this.ShiJiJiaoHuo1.Text = ((double)e.Command.Parameters[27].Value).ToString();
                    this.ShiJiJiaoHuo2.Text = ((double)e.Command.Parameters[28].Value).ToString();
                    this.ShiJiJiaoHuo3.Text = ((double)e.Command.Parameters[29].Value).ToString();
                    this.ShiJiJiaoHuo4.Text = ((double)e.Command.Parameters[30].Value).ToString();
                    this.ShiJiJiaoHuo5.Text = ((double)e.Command.Parameters[31].Value).ToString();
                    this.ShiJiJiaoHuo6.Text = ((double)e.Command.Parameters[32].Value).ToString();
                    this.ShiJiJiaoHuo7.Text = ((double)e.Command.Parameters[33].Value).ToString();
                    this.ShiJiJiaoHuo8.Text = ((double)e.Command.Parameters[34].Value).ToString();                    
                    this.ShiJiJiaoHuo9.Text = ((double)e.Command.Parameters[35].Value).ToString();

                    this.ShiJiJiaoHuo10.Text = ((double)e.Command.Parameters[36].Value).ToString();
                    this.ShiJiJiaoHuo11.Text = ((double)e.Command.Parameters[37].Value).ToString();
                    this.ShiJiJiaoHuo12.Text = ((double)e.Command.Parameters[38].Value).ToString();
                    this.ShiJiJiaoHuo13.Text = ((double)e.Command.Parameters[39].Value).ToString();
                    this.ShiJiJiaoHuo14.Text = ((double)e.Command.Parameters[40].Value).ToString();
                    this.ShiJiJiaoHuo15.Text = ((double)e.Command.Parameters[41].Value).ToString();
                    this.ShiJiJiaoHuo16.Text = ((double)e.Command.Parameters[42].Value).ToString();
                    this.ShiJiJiaoHuo17.Text = ((double)e.Command.Parameters[43].Value).ToString();
                    this.ShiJiJiaoHuo18.Text = ((double)e.Command.Parameters[44].Value).ToString();


                    this.LeiBie.Text = (string)e.Command.Parameters[45].Value;
                    this.GuiGe.Text = (string)e.Command.Parameters[46].Value;
                    this.GongHuo.Text = (string)e.Command.Parameters[47].Value;
                    GetPlantData((string)e.Command.Parameters[45].Value, (string)e.Command.Parameters[47].Value, this.NianDu.Text, this.JiDu.Text);
                }
            }
            else if (this.ZBID.Text != "0")
            {
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.NianDu.Text = ((int)e.Command.Parameters[5].Value).ToString();
                    this.JiDu.Text = ((byte)e.Command.Parameters[6].Value).ToString();
                    this.NianJiDu.Text = this.NianDu.Text + "年" + this.JiDu.Text + "季度";
                    this.LBID.Text = ((int)e.Command.Parameters[3].Value).ToString();
                    this.LeiBie.Text = (string)e.Command.Parameters[45].Value;
                    this.GuiGe.Text = (string)e.Command.Parameters[46].Value;
                    this.GHID.Text = ((int)e.Command.Parameters[4].Value).ToString();
                    this.GongHuo.Text = (string)e.Command.Parameters[47].Value;
                    this.ShuLiang.Text = ((double)e.Command.Parameters[7].Value).ToString();
                    this.JiaoHuo.Text = this.ShuLiang.Text;
                    this.DanJia.Text = ((double)e.Command.Parameters[8].Value).ToString();
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            this.BianHao.Text = this.BianHao.Text.Trim();
            if (this.BianHao.Text == "")
            {
                this.Message.Text = "·请录入“合同编号”";
                return; ;
            }
            DateTime d1, d2;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Message.Text = "·“签订日期”录入有误";
                return; ;
            }
            if (!DateTime.TryParse(this.Date2.Text, out d2))
            {
                this.Message.Text = "·“交货时间”录入有误";
                return; ;
            }
            decimal shuliang;
            if (!decimal.TryParse(this.ShuLiang.Text, out shuliang) || shuliang <= 0)
            {
                this.Message.Text = "·“数量”录入有误";
                return; ;
            }
            decimal danjia;
            if (!decimal.TryParse(this.DanJia.Text, out danjia) || danjia <= 0)
            {
                this.Message.Text = "·“单价”录入有误";
                return; ;
            }
            int i;
            decimal[] jiaohuo = new decimal[19];
            decimal[] shijijiaohuo = new decimal[19];
            decimal total = 0;
            if (!decimal.TryParse(this.JiaoHuo.Text, out jiaohuo[0]) || jiaohuo[0] <= 0)
            {
                this.Message.Text = "·“交货数量”录入有误";
                return; ;
            }
            for (i = 1; i < 13; i++)
            {
                if (!decimal.TryParse(((TextBox)this.FindControl("JiaoHuo" + i)).Text, out jiaohuo[i]) || jiaohuo[i] < 0)
                {
                    this.Message.Text = "·计划交货明细中存在无效数据或未填写";
                    return; ;
                }
              
                total += jiaohuo[i];
            }
            if (total != jiaohuo[0])
            {
                this.Message.Text = "·交货明细总和(" + total + ")与交货数量不符(" + jiaohuo[0] + ")";
                return; ;
            }

            //开始保存数据
            if (this.HTID.Text != "0")
            {
                //编号,中标ID,原料ID,供货ID,年度,季度,数量,单价,长度,签订日期,交货时间,运输方式,备注,交货数量
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["ID"].DefaultValue = this.HTID.Text;
                pc["编号"].DefaultValue = this.BianHao.Text;
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                if (LeiBie.Text == "套管光管" || LeiBie.Text == "套管光管(N80)")
                {
                    this.ChangDu.Text = "9.6~10.3";
                }
                if (LeiBie.Text == "油管光管" || LeiBie.Text == "油管光管(N80)")
                {
                    this.ChangDu.Text = "9.2~9.7";
                }
                pc["长度"].DefaultValue = this.ChangDu.Text;
                pc["签订日期"].DefaultValue = d1.ToShortDateString();
                pc["交货时间"].DefaultValue = d2.ToShortDateString();
                pc["运输方式"].DefaultValue = this.YunShu.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["交货数量"].DefaultValue = jiaohuo[0].ToString();
                for (i = 1; i < 13; i++)
                {
                    if (!decimal.TryParse(((TextBox)this.FindControl("ShiJiJiaoHuo" + i)).Text, out shijijiaohuo[i]) || shijijiaohuo[i] < 0)
                    {
                        this.Message.Text = "·实际交货明细中存在无效数据或未填写";
                        return; ;
                    }
                    pc["交货数量" + i].DefaultValue = jiaohuo[i].ToString();                   
                    pc["实际交货数量" + i].DefaultValue = shijijiaohuo[i].ToString();
                   
                }
                for (i = 13; i < 19; i++)
                {
                    if (!decimal.TryParse(((TextBox)this.FindControl("ShiJiJiaoHuo" + i)).Text, out shijijiaohuo[i]) || shijijiaohuo[i] < 0)
                    {
                        this.Message.Text = "·实际交货明细中存在无效数据或未填写";
                        return; ;
                    }
                    pc["实际交货数量" + i].DefaultValue = shijijiaohuo[i].ToString();
                }
                pc["交货数量" + 10].DefaultValue = jiaohuo[10].ToString();
                pc["交货数量" + 11].DefaultValue = jiaohuo[11].ToString();
                pc["交货数量" + 12].DefaultValue = jiaohuo[12].ToString(); 
              
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
            else if (this.ZBID.Text != "0")//新合同
            {
                //编号,中标ID,原料ID,供货ID,年度,季度,数量,单价,长度,签订日期,交货时间,运输方式,备注,交货数量
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["编号"].DefaultValue = this.BianHao.Text;
                pc["中标ID"].DefaultValue = this.ZBID.Text;
                pc["原料ID"].DefaultValue = this.LBID.Text;
                pc["供货ID"].DefaultValue = this.GHID.Text;
                pc["年度"].DefaultValue = this.NianDu.Text;
                pc["季度"].DefaultValue = this.JiDu.Text;
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                if (LeiBie.Text == "套管光管" || LeiBie.Text == "套管光管(N80)")
                {
                    this.ChangDu.Text = "9.6~10.3";
                }
                if (LeiBie.Text == "油管光管" || LeiBie.Text == "油管光管(N80)")
                {
                    this.ChangDu.Text = "9.2~9.7";
                }
                pc["长度"].DefaultValue = this.ChangDu.Text;
                pc["签订日期"].DefaultValue = d1.ToShortDateString();
                pc["交货时间"].DefaultValue = d2.ToShortDateString();
                pc["运输方式"].DefaultValue = this.YunShu.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["交货数量"].DefaultValue = jiaohuo[0].ToString();
                for (i = 1; i < 13; i++)
                {
                    pc["交货数量" + i].DefaultValue = jiaohuo[i].ToString();                
                }              
                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[0].Value))
            {
                this.HTID.Text = ((int)e.Command.Parameters[0].Value).ToString();
            }
        }
        //参考计划明细赋初值
        protected void GetPlantData(string 原料名称,string 供货单位,string 年度,string 季度)
        {
            GetXun(年度, DateTime.Now.Month.ToString());
            if (this.Message.Text == ".该扎帐月没有设置")
            {              
                for (int i = 1; i < 10; i++)
                {
                    ((TextBox)this.FindControl("TextBox" + i)).Text = "0";
                    ((TextBox)this.FindControl("TextBox" + i)).ReadOnly = true;
                    ((TextBox)this.FindControl("TextBox" + i)).CssClass = "blank_left";
                }     
                return;
            }
            int jidu = int.Parse(季度);
            if (jidu == 1)
            {
                m1 = 1;
                m2 = 2;
                m3 = 3;
            }
            if (jidu == 2)
            {
                m1 = 4;
                m2 = 5;
                m3 = 6;
            }
            else if (jidu == 3)
            {
                m1 = 7;
                m2 = 8;
                m3 = 9;
            }
            else if (jidu == 4)
            {
                m1 = 10;
                m2 = 11;
                m3 = 12;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string sql = @"
            select dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份1,'上旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份1,'中旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份1,'下旬'),
            dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份2,'上旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份2,'中旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份2,'下旬'),
            dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份3,'上旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份3,'中旬'),dbo.当旬合同到货量('@原料名称','@供货单位',@年度,@月份3,'下旬')";
        
            sql = sql.Replace("@原料名称", 原料名称);
            sql = sql.Replace("@供货单位", 供货单位);
            sql = sql.Replace("@年度", 年度);        
            sql = sql.Replace("@月份1", m1.ToString());
            sql = sql.Replace("@月份2", m2.ToString());
            sql = sql.Replace("@月份3", m3.ToString());
    
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                this.TextBox1.Text = Reader.GetDecimal(0).ToString("0");
                this.TextBox2.Text = Reader.GetDecimal(1).ToString("0");
                this.TextBox3.Text = Reader.GetDecimal(2).ToString("0");
                this.TextBox4.Text = Reader.GetDecimal(3).ToString("0");
                this.TextBox5.Text = Reader.GetDecimal(4).ToString("0");
                this.TextBox6.Text = Reader.GetDecimal(5).ToString("0");
                this.TextBox7.Text = Reader.GetDecimal(6).ToString("0");
                this.TextBox8.Text = Reader.GetDecimal(7).ToString("0");
                this.TextBox9.Text = Reader.GetDecimal(8).ToString("0");
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
            for (int i = 1; i < 10; i++)
            {
               ((TextBox)this.FindControl("TextBox"+i)).ReadOnly = true;
               ((TextBox)this.FindControl("TextBox"+i)).CssClass = "blank_left";
            }     
        }
        public void GetXun(string yy, string mm)
        {

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select 月初日期,上旬日期,中旬日期,月末日期 from 供应处生产月设置 where 年度='" + yy + "' and 月份='" + mm + "'", Conn);
            sda.Fill(ds);
            try
            {
                d1 = DateTime.Parse(ds.Tables[0].Rows[0][0].ToString());
                d2 = DateTime.Parse(ds.Tables[0].Rows[0][1].ToString());
                d3 = DateTime.Parse(ds.Tables[0].Rows[0][2].ToString());
                d4 = DateTime.Parse(ds.Tables[0].Rows[0][3].ToString());
            }
            catch
            {
                this.Message.Text = ".该扎帐月没有设置";
            }
        }
        
    }
}
